import pygame
import random

pygame.init()
pygame.font.init()
pygame.mixer.music.load("song.mp3")

display_width = 600
display_height= 600

white = (255,255,255)
black = (0,0,0)
red = (200,0,0)
light_red =(255,0,0)
green = (0,200,0)
light_green =(0,255,0)
gunc=(237,189,24)
yellow = (255,255,0)
light_yellow =(255,255,60)

pause = False
direction = "right"

start_x = display_width/2
start_y = display_height * 0.75


gameDisplay  = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Space Wars")
clock =  pygame.time.Clock()
RocketImg = pygame.image.load('rocket2.png')
(roc_width,roc_height) = RocketImg.get_rect().size
planetImg = pygame.image.load('planet.png')
background = pygame.image.load('background.png')
introback = pygame.image.load('intro.png')
control = pygame.image.load('control.png')
earth = pygame.image.load('earth.png')

enemy = pygame.image.load('enemy2.png')
(enemy_width, enemy_height) = enemy.get_rect().size

pygame.display.set_icon(enemy)

randEnemyX = random.randint(0,display_width - enemy_width)
randEnemyY = random.randint(0 , display_height/2)

obstacle = pygame.image.load("obstacle1.png")
scores=0

pygame.display.update()

def rocket(x,y):
    gameDisplay.blit(RocketImg, (x,y))

def shoot():
    shoot_x= int(start_x+(roc_width/2))
    shoot_y = int(start_y)
    fire= True
    change =-2
    arr = list()
    count = 0

    while fire==True:
        for event in pygame.event.get():
            if event== pygame.QUIT:
                pygame.quit()
                quit()
        shoot_y += change
        if randEnemyY <= shoot_y <= randEnemyY+enemy_height:
            if randEnemyX <= shoot_x <= randEnemyX + enemy_width:
                change_space_ship()
                global scores
                scores+= 1

        pygame.draw.circle(gameDisplay,gunc,(shoot_x,shoot_y),5)



        if shoot_y <= 0 :
            fire= False




        pygame.display.update()

def change_space_ship():
    global randEnemyX
    global randEnemyY
    randEnemyX = random.randint(0, display_width - enemy_width)
    randEnemyY = random.randint(0, display_height / 2)
    pygame.display.update()



def unpause():
    pygame.mixer.music.unpause()
    global pause
    pause = False


def intro():
    gameDisplay.blit(introback,(0,0))
    largeText = pygame.font.Font("comicbd.ttf", 75)
    #TextSurf, TextRect = text_objects("Space Wars", largeText, white)
    #TextRect.center = ((display_width / 2), (display_height / 4))
    #gameDisplay.blit(TextSurf, TextRect)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()


        button("Start", 100, 350, 120, 50, green, light_green, game_loop)
        button("Quit", 450, 350, 100, 50, red, light_red, quit_game)
        button("Game Rules",270,350,130,50,yellow,light_yellow,story)

        pygame.display.update()

def story():
    while True:
        gameDisplay.fill(black)
        gameDisplay.blit(control,(0,0))
        while True :
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
            button("Home",300,500,60,40,yellow,light_yellow,intro)
            pygame.display.update()





def paused():

    pygame.mixer.music.pause()


    largeText = pygame.font.Font("comicbd.ttf", 75)
    TextSurf, TextRect = text_objects("Paused", largeText, white)
    TextRect.center = ((display_width / 2), (display_height / 4))
    gameDisplay.blit(TextSurf, TextRect)
    while pause:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        # gameDisplay.fill(white)

        button("Continue", 100, 400, 120, 50, green, light_green, unpause)
        button("Quit", 450, 400, 100, 50, red, light_red, quit_game)
        button("Home",275,400,100,50,yellow,light_yellow,intro)

        pygame.display.update()

def score(score):
    font= pygame.font.SysFont(None,25)
    text= font.render("Score: "+ str(score),True,gunc)
    gameDisplay.blit(text,(0,0))


def text_objects(text, font, color):
    textSurface = font.render(text, True,color )
    return textSurface, textSurface.get_rect()

def quit_game():
    pygame.quit()
    quit()


def button(text ,x, y, w, h, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac , (x, y, w, h))
        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(gameDisplay, ic , (x, y, w, h))

    SmallText = pygame.font.Font("comic.ttf", 20)
    textSurf, textRect = text_objects(text , SmallText, black)
    textRect.center = ((x + (w / 2)), (y + (h / 2)))
    gameDisplay.blit(textSurf, textRect)

def crash():
    #gameDisplay.fill(black)
    largeText = pygame.font.Font("comicbd.ttf", 75)
    TextSurf, TextRect = text_objects("Game Over", largeText, white)
    TextRect.center = ((display_width / 2), (display_height / 4))
    gameDisplay.blit(TextSurf, TextRect)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        button("Play Again", 100, 400, 120, 50, green, light_green, game_loop)
        button("Quit", 450, 400, 100, 50, red, light_red, quit_game)
        button("Home", 270, 400, 100, 50, yellow, light_yellow, intro)
        pygame.display.update()


def create_enemy(x,y):
    gameDisplay.blit(enemy, (x,y))


def game_loop():
    pygame.mixer.music.play(-1)
    global pause
    global start_x
    (ob_width, ob_height) = obstacle.get_rect().size
    ob_x = random.randint(0, display_width - ob_width)
    ob_y = -600
    ob_speed =18
    global scores
    scores = 0

    gameExit = False
    changex = 0

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    changex = -10
                elif event.key == pygame.K_RIGHT:
                    changex = 10
                elif event.key == pygame.K_UP:
                    pass
                elif event.key == pygame.K_DOWN:
                    pass
                elif event.key == pygame.K_p:
                    pause = True
                    paused()
                elif event.key == pygame.K_SPACE:
                    shoot()


            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    changex = 0

        gameDisplay.fill(black)
        gameDisplay.blit(background, (0,0))
        gameDisplay.blit(planetImg,(150,150))
        gameDisplay.blit(earth,(300,400))
        start_x += changex
        rocket(start_x, start_y)
        create_enemy(randEnemyX, randEnemyY)
        gameDisplay.blit(obstacle,(ob_x,ob_y))

        ob_y+= ob_speed# make the object come down by adding to the value of y
        score(scores)


        if start_x > display_width - roc_width :
           changex = -1
        elif start_x<0:
            changex =1

        if display_height < ob_y:
            ob_y = 0-ob_height
            ob_x = random.randint(0,display_width-ob_width)

        if start_y in range(ob_y,ob_y+ ob_height):
            if start_x + roc_width > ob_x and start_x < ob_x+ob_width:
                crash()

        pygame.display.update()
        clock.tick(60)




intro()
game_loop()
pygame.quit()
quit()
